package com.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bean.*;
import com.dao.SeckillMapper;
import com.service.IOrderService;
import com.service.impl.OrderServiceImpl;
import com.service.impl.UserServiceImpl;
import com.shiro.token.JWTToken;
import org.apache.logging.log4j.util.Strings;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;

/**
 * @author shkstart
 * @create 2022-06-22-14:34
 */

@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    UserServiceImpl userService;

    @Resource
    OrderServiceImpl orderService;

    @Resource
    SeckillMapper seckillMapper;



    /**
     * 编辑用户信息
     */
    @PostMapping("/registUser")
    public Msg registUser(
            @RequestParam("nickName") String nickName,
            @RequestParam("phone") String phone,
            @RequestParam(  "address") String address,
            @RequestHeader("x-access-token") String jwt){

        //把jwt转化为Token，因为已经走了jwt认证了不需要判空和校验
        JWTToken jwtToken = new JWTToken(jwt);
        //解析jwt查找到user对象
        String email = jwtToken.getEmail();
        User user = userService.findByEmail(email);
        Integer userId = user.getUserId();
        if(!Strings.isEmpty(nickName)) user.setNickName(nickName);
        if(!Strings.isEmpty(phone)) user.setPhone(phone);
        if(!Strings.isEmpty(address)) user.setNickName(address);
        userService.updateByUserId(nickName,phone,address,userId);
        return Msg.success();
    }

    /**
     * 上传头像
     */
    @PostMapping("/headUp")
    public Msg uploadImg(@RequestParam Integer userId,
                         @RequestParam  MultipartFile headerImg) throws IOException {
        //保存到文件服务器，OSS服务器
        try{
            if(!headerImg.isEmpty()) {
                //获取用户
                User user = userService.getById(userId);
                //将图片上传到服务器本地磁盘中 拼接方式
                headerImg.transferTo(new File("E:\\AdminCache\\" + user.getNickName() + ".jpg"));
                //修改数据库中的picture
                String imgPath="http://localhost:8090/file/"+user.getNickName()+".jpg";
                user.setPicture(imgPath);
                userService.updateById(user);
            }
            return Msg.success();
        }
        catch (Exception e){
            return Msg.fail();
        }
    }


    /**
     * 显示参与的订单
     */
    @GetMapping("/orderPage")
    public Msg orderPage(
                                   @RequestHeader("x-access-token") String jwt
    ){
        //把jwt转化为Token，因为已经走了jwt认证了不需要判空和校验
        JWTToken jwtToken = new JWTToken(jwt);
        //解析jwt查找到user对象
        String email = jwtToken.getEmail();
        User user = userService.findByEmail(email);


        List<Orders> orderByUserId = orderService.getOrderByUserId(user);


        return Msg.success().add("result",orderByUserId);
    }

}